package com.hbx.controller;


import com.hbx.aspect.annotation.SensitiveFilter;
import com.hbx.constant.ResponseStateConstant;
import com.hbx.dto.blog.InsertDto;
import com.hbx.dto.blog.SearchBlogDto;
import com.hbx.service.BlogService;
import com.hbx.utils.Result;
import com.hbx.vo.blog.MyBlogVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hbx
 * @since 2022-01-15
 */
@RestController
@RequestMapping("/blog")
@Api(tags = "博客处理器")
public class BlogController {

    @Autowired
    private BlogService blogService;

    /**
     * 查询我的博客
     * @param pageSize
     * @param userId
     * @return
     */
    @RequestMapping(value = "/getMyBlog",method = RequestMethod.GET)
    @ApiOperation("查询我的博客")
    public Result<MyBlogVo> getMyBlog(@RequestParam("pageSize") Integer pageSize,
                                      @RequestParam("userId") Integer userId){
        Result<MyBlogVo> r = new Result<>();
        if (ObjectUtils.isEmpty(pageSize)){
            r.setCode(ResponseStateConstant.PARAMETER_ERROR);
            r.setMsg("查询条数为必传项");
            return r;
        }
        if (ObjectUtils.isEmpty(userId)){
            r.setCode(ResponseStateConstant.PARAMETER_ERROR);
            r.setMsg("userId为必传项");
            return r;
        }
        blogService.getMyBlog(pageSize,userId,r);
        return r;
    }

    /**
     * 查询所有博客分页
     * @param search
     * @return
     */
    @RequestMapping(value = "/getBlogList",method = RequestMethod.POST)
    @ApiOperation("查询博客列表")
    public Result getBlogList(@RequestBody SearchBlogDto search){
        Result r = new Result();
        if (ObjectUtils.isEmpty(search)){
            r.setCode(400).setMsg("请求参数异常");
            return r;
        }
        if (search.getPageNum() == null){
            search.setPageNum(1);
        }
        if (search.getPageSize() == null){
            r.setCode(400).setMsg("pageSize为必传项");
            return r;
        }
        blogService.getBlogListPage(search, r);
        return r;
    }

    /**
     * 查询博客详情
     * @param blogId
     * @return
     */
    @RequestMapping(value = "/getBlogDetail",method = RequestMethod.GET)
    @ApiOperation("查询博客详情")
    public Result getBlogDetail(@RequestParam("blogId") Integer blogId){
        Result r = new Result();
        if (ObjectUtils.isEmpty(blogId)){
            r.setCode(400).setMsg("请求参数异常");
            return r;
        }
        blogService.getBlogDetail(blogId, r);
        return r;
    }

    /**
     * 点赞或取消点赞
     * @param blogId
     * @return
     */
    @RequestMapping(value = "/addNiu",method = RequestMethod.GET)
    @ApiOperation("点赞或取消点赞")
    public Result addNiu(@RequestParam("blogId") Integer blogId){
        Result r = new Result();
        if (blogId == null){
            r.setCode(400).setMsg("blogId为必传项");
            return r;
        }
        blogService.addNiu(blogId, r);
        return r;
    }

    /**
     * 添加博客
     * @param dto
     * @param result
     * @return
     */
    @RequestMapping(value = "/addBlog",method = RequestMethod.POST)
    @ApiOperation("添加博客")
    @SensitiveFilter
    public Result insertBlog(@RequestBody @Valid InsertDto dto, BindingResult result){
        Result r = new Result();
        if (result.hasErrors()){
            r.setCode(400).setMsg(result.getFieldError().getDefaultMessage());
            return r;
        }
        blogService.insertBlog(dto, r);
        return r;
    }

    /**
     * 删除博客
     * @param blogId
     * @return
     */
    @RequestMapping(value = "/removeBlog",method = RequestMethod.GET)
    @ApiOperation("用户删除博客")
    public Result removeBlog(@RequestParam("blogId") Integer blogId){
        Result r = new Result();
        if (blogId == null){
            r.setCode(400).setMsg("blogId为必传项");
            return r;
        }
        blogService.removeBlog(blogId, r);
        return r;
    }

}

